﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ca2simulator.shared
{
    [Serializable()]
    public class Lattice
    {
        private readonly CellState _initialState;
        private readonly int _rows, _columns;
        // Cell[] is kept public in case you want REALLY fast access times...
        // However you will have to do your own bounds checking and row/colums translation
        public Cell[] _Cells;

        /* Properties */
        public int Rows { get { return _rows; } }
        public int Columns { get { return _columns; } }
        public CellState InitialState { get { return _initialState; } }
        public Cell this[int r, int c, bool bounds=false] 
        { 
            get 
            {
                if (bounds)
                {
                    // Only do bounds checking if we want to, otherwise don't for speed
                    r %= Rows;
                    r = r<0 ?r+Rows:r;
                    c %= Columns;
                    c = c < 0 ? c +Columns : c;
                }
                return _Cells[r*_columns+c]; 
            }
            set 
            {
                r = r >= _rows ? r % _rows : r;
                c = c >= _columns ? c % _columns : c;
                _Cells[r*_columns + c] = value; 
            }
        }

        public Lattice(CellState initialState, int rows, int columns)
        {
            if (rows <= 0 || columns <= 0)
            {
                throw new ArgumentException("Lattice() - rows or columns were <= 0");
            }
            _Cells = new Cell[rows*columns];
            _initialState = initialState;
            _rows = rows;
            _columns = columns;
            setAllCells(initialState);
            unsafe
            {
                int size = sizeof(CellState) * rows * columns;
            }
        }

        private void setAllCells(CellState state)
        {
            for (int r = 0; r < _rows; r++)
            {
                for (int c = 0; c < _columns; c++)
                {
                    _Cells[r*_columns+c].CellState = state;
                }
            }
        }

        
    }
}
